package com.juzipi.demo.controller;


import com.juzipi.demo.pojo.Article;
import com.juzipi.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
public class ArticlrController {


    @Autowired
    private ArticleService articleService;


    @GetMapping
    public ResponseEntity<List<Article>> queryArticleList(){

        List<Article> articles = articleService.queryArticleList();
        if (CollectionUtils.isEmpty(articles)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.OK).body(articles);

    }


    @GetMapping("{id}")
    public ResponseEntity<Article> queryArticleById(@PathVariable Integer id){
        Article article = articleService.queryArticleById(id);
        if (StringUtils.isEmpty(article)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        return ResponseEntity.ok(article);
    }


    /**
     * 查询并返回分页结果集，区别是用 map 装的返回结果集
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("page")
    public ResponseEntity<Map<String,Object>> queryArticleByPage(
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
    ){
        Map<String,Object> articles = articleService.queryArticleByPage(pageNum,pageSize);//传入页码和分页大小
        if (StringUtils.isEmpty(articles)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(articles);

    }








}
